import LayVmodelBase from "./LayVmodelBase.js"
import { logger, tools, runtime } from "./tools.js";

/**Layui评分绑定 */
class LayRateVmodel extends LayVmodelBase {
    config = null;

    constructor(elem, config) {
        super(elem);
        this.config = config;
        // this.transDirection = "toValue";

        this.checkFormFilter();
        this.ensureName();
        this.ensureFilter();
    }

    onDisabledChange(disabled) {
        let old = this.config.readonly;
        this.config.readonly = disabled;
        this.render();
        this.config.readonly = old;
    }

    /**[override] */
    init() {
        if (!super.init()) return;
        var tis = this;
        tis.config.elem = tis.element;
        tis.config.value = tis.config.value || tis.vm.value() || 0;

        var innerEvent = tis.config.choose;
        tis.config.choose = function (value) {
            if (value !== tis.vm.value()) {
                tis.vm.value(value);
            }
            innerEvent && innerEvent.call(tis, ...arguments);
        }

        tis.vm.value.subscribe((newValue) => {
            tis.render();
        });
        let rate = tools.laymod('rate');
        rate.render(this.config);
        this.inited = true;
    }

    render() {
        let cfg = Object.assign({}, this.config);
        cfg.elem = this.element;
        cfg.value = this.vm.value();
        cfg.choose = this.config.choose;
        
        let rate = tools.laymod('rate');
        rate.render(cfg);
    }

    viewModelInit() {
        super.viewModelInit();
        this.vm.value = ko.observable(0);
    }

}


export default LayRateVmodel;